<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>发布报告</title>
    <script src="../js/jquery.min.js"></script>
    <link rel="stylesheet" href="../../../static/layui-v2.6.8/layui/css/layui.css" media="all">

</head>
<body style="background-color:#f2f2f2;">


  <div class="layui-bg-gray" style="padding: 10px;">
    <div class="layui-row layui-col-space15">
      <div class="layui-col-md12">
        <div class="layui-card">
          <div class="layui-card-body" >
            <button class="layui-btn" id="getreport">立即提交</button>
            <button class="layui-btn" id='savedraft'>保存草稿</button>
            <button class="layui-btn" id="demo1">
                我的草稿
            <i class="layui-icon layui-icon-down layui-font-12"></i>
            </button>
          </div>
          <hr class="layui-border-black">
          <div layui-input class="layui-card-body" style="text-align: center;">
            <input type="text" style=" width: 80%; font-size: 36px;overflow-y:hidden;text-align: center; background-color:transparent;border:0 ;" maxlength="15";name = 'report_title'; placeholder="请输入标题" >
          <hr class="layui-border-black">
          <div class="layui-card-body" id='edit-body'>
            <textarea id="text" style="width: 100%;height:inherit; resize:none;border: none;font-size: 24px;overflow-y:hidden"placeholder="请输入内容" maxlength='1500'></textarea>
          </div>
        </div>
      </div>
  
  
<script src="../../../static/layui-v2.6.8/layui/layui.js"></script>
<script>
  


  var $ = layui.jquery
  //获取当前窗口高度
  var document_body = $(document).height()
  //更改编辑器body总高度为窗口高度的百分比
  $('#edit-body').height(document_body*0.8)
  $('#getreport').on('click',function(){
    var report =$('textarea').val()
    var title = $('input').val()
    if(title ===""){
      alert("标题不能为空")
    }else if(report===""){
      alert('请输入内容')
    }else{
      var content = {
        "report" : report,
        "title" : title
      }
      $.ajax({
        url: '/workreport/add',
        type:'post',
        data:JSON.stringify(content),
        dataType:'json',
        contentType:'application/json',
        success: function(res){
          if (res.code ==200){
            alert('添加成功')
          }else{
            alert(res.error)
          }
        }
      })
  }

});
    $('#savedraft').on('click',function(){
    var report =$('textarea').val()
    var title = $('input').val()
    
    //判断内容是否为空,如果是空则提示
    if(title ===""){
      alert("标题不能为空")
    }else if(report===""){
      alert('请输入内容')
    }else{
      var draft = {
        'title' :title,
        'report' :report
      }
      //发送请求
      $.ajax({
        url: '/workreport/draft',
        type:'post',
        data:JSON.stringify(draft),
        dataType:'json',
        contentType:'application/json',
        success: function(res){
          if (res.code ==200){
            alert('保存成功')
            location.reload();
            $('input').val('')
            $('textarea').val('')
            
          }
        }
      })
  }
  })
  
  //下拉菜单
  layui.use('dropdown', function(){
  var dropdown = layui.dropdown
  var $ = layui.$
  $.ajax({
    url: '/workreport/getdraft',
    type:'get',
    success: function(values){
      if (values.code ==200){
        var titles = values.data
        dropdown.render({
        elem: '#demo1' //可绑定在任意元素中，此处以上述按钮为例
        ,data: titles
        ,id: 'demo1'
        //菜单被点击的事件
        ,click: function(obj){
          var rid = obj.id
          var title = obj.title
          var parame ={
            "id" : rid,
            'title' : title
          }
          $.ajax({
            url:'/workreport/getdraft',
            type:'post',
            dataType:'json',
            contentType:'application/json',
            data:JSON.stringify(parame),
            success:function(res){
              if(res.code == 200){
              var draft = res.data

              $('input').val(draft.title)
              $('textarea').val(draft.report)
              }
            }
          })
        }
      });
      }
    }
  })
});

</script>
    
</body>
</html>